package com.git.hui.yinshua.biz.rest.admin.goods;

import com.git.hui.yinshua.api.model.ExceptionUtil;
import com.git.hui.yinshua.api.model.ListRsp;
import com.git.hui.yinshua.api.model.PageReq;
import com.git.hui.yinshua.api.model.ResVo;
import com.git.hui.yinshua.api.model.Status;
import com.git.hui.yinshua.api.model.permission.Permission;
import com.git.hui.yinshua.api.model.permission.UserAccessRole;
import com.git.hui.yinshua.biz.goods.service.CategoryService;
import com.git.hui.yinshua.biz.rest.models.goods.req.CategoryQueryReq;
import com.git.hui.yinshua.biz.rest.models.goods.req.CategoryReq;
import com.git.hui.yinshua.biz.rest.models.goods.rsp.CategoryDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author YiHui
 * @date 2024/6/12
 */
@Permission(role = UserAccessRole.ADMIN)
@RequestMapping(path = "admin/category")
@RestController
public class AdminCategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping(path = "list")
    public ResVo<ListRsp<CategoryDto>> list(CategoryQueryReq page) {
        page.autoInitReq();
        ListRsp<CategoryDto> list = categoryService.categoryList(page);
        return ResVo.ok(list);
    }

    @RequestMapping(path = "add")
    public ResVo<CategoryDto> add(@RequestBody CategoryReq req) {
        if (StringUtils.isBlank(req.getCategory())) {
            throw ExceptionUtil.of(Status.StatusEnum.ILLEGAL_ARGUMENTS_MIXED, "类目名不能为空");
        }

        if (req.getEnable() == null) {
            req.setEnable(false);
        }
        return ResVo.ok(categoryService.save(req));
    }

    @RequestMapping(path = "update")
    public ResVo<Boolean> update(@RequestBody CategoryReq req) {
        if (req.getCid() == null) {
            throw ExceptionUtil.of(Status.StatusEnum.ILLEGAL_ARGUMENTS_MIXED, "类目id不能为空");
        }

        return ResVo.ok(categoryService.update(req));
    }

    @RequestMapping(path = "enabled")
    public ResVo<Boolean> updateState(Integer cid, Boolean enabled) {
        if (cid == null || enabled == null) {
            throw ExceptionUtil.of(Status.StatusEnum.ILLEGAL_ARGUMENTS_MIXED, "类目or状态不能为空");
        }

        return ResVo.ok(categoryService.enable(cid, enabled));
    }

    @RequestMapping(path = "remove")
    public ResVo<Boolean> remove(Integer cid) {
        if (cid == null) {
            throw ExceptionUtil.of(Status.StatusEnum.ILLEGAL_ARGUMENTS_MIXED, "请指定需要删除的类目");
        }

        return ResVo.ok(categoryService.remove(cid));
    }


}
